package com.zdkj.subway.check.controller.stationTiecketManager.xianWngCJXXQR;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.core.util.R;
import com.zdkj.common.security.annotation.Inner;
import com.zdkj.subway.check.controller.CommonController;
import com.zdkj.subway.check.entity.Page;
import com.zdkj.subway.check.entity.SwPmReportPath;
import com.zdkj.subway.check.entity.SwStReportErrorAlterAppayDetail;
import com.zdkj.subway.check.entity.SwStReportErrorAlterApply;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwPmReportPathMapper;
import com.zdkj.subway.check.mapper.SwStReportErrorAlterAppayDetailMapper;
import com.zdkj.subway.check.mapper.SwStReportErrorAlterAppayMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.service.impl.subwayRat.reportMondify.ReportMistakeInfoServiceImpl;
import com.zdkj.subway.check.util.DateFormat;
import com.zdkj.subway.check.util.JdbcUtil;
import com.zdkj.subway.check.util.UserInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 报表差错信息
 */
@RestController
@RequestMapping("/xw")
@Slf4j
public class Xw_ReportMistakeInfoCtrl {

    @Resource
    private ReportMistakeInfoServiceImpl reportMistakeInfoServiceImpl;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SwStReportErrorAlterAppayMapper swStReportErrorAlterAppayMapper;
    @Resource
    private SwStReportErrorAlterAppayDetailMapper swStReportErrorAlterAppayDetailMapper;
    @Resource
    private SwStReportErrorAlterAppayMapper reportMistakeInfoMapper;
    @Resource
    private SwPmReportPathMapper swPmReportPathMapper;
    @Autowired
    private CommonController commonController;

    /**
     * 获取报表差错信息列表
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/get_xw_ReportErrorList")
    @ApiOperation(value = "获取报表差错信息列表", notes = "获取报表差错信息列表")
    public Page get_xw_ReportErrorList(Page page, @RequestParam Map<String,String> param){

        List<Map<String,Object>> reportErrorAlterAppays= reportMistakeInfoMapper.get_xw_ReportErrorList(param);
        page.setData(reportErrorAlterAppays,true);
        return page;
    }

    /**
     *  报表涉及人员
     * @param jsonObject
     * @return
     */
    @RequestMapping("/get_xw_SwStReportErrorAlterAppayDetail")
    @ApiOperation(value = "报表涉及人员", notes = "报表涉及人员")
    public Page get_xw_ReportErrorList(Page page, @RequestBody JSONObject jsonObject){
        SwStReportErrorAlterAppayDetail swStReportErrorAlterAppayDetail = JSONObject.parseObject(jsonObject.toJSONString(), SwStReportErrorAlterAppayDetail.class);
        QueryWrapper<SwStReportErrorAlterAppayDetail> queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(swStReportErrorAlterAppayDetail);
		queryWrapper.orderByAsc("STATION_PERSONR_CODE");
        List<SwStReportErrorAlterAppayDetail> reportErrorAlterAppays= swStReportErrorAlterAppayDetailMapper.selectList(queryWrapper);
        page.setData(reportErrorAlterAppays);
        return page;
    }

    /**
     *  修改报表状态
     * @param swStReportErrorAlterApply
     * @return
     */
    @PostMapping("/updateXwReportInfos")
    @ApiOperation(value = "修改报表状态", notes = "修改报表状态")
    @Inner
    public R get_xw_ReportErrorList(@Valid @RequestBody SwStReportErrorAlterApply swStReportErrorAlterApply) {
        SwStReportErrorAlterApply sws = new SwStReportErrorAlterApply();
        sws.setIsModify("1");
        LambdaUpdateWrapper<SwStReportErrorAlterApply> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SwStReportErrorAlterApply::getLineCode, swStReportErrorAlterApply.getLineCode())
                .eq(SwStReportErrorAlterApply::getStationCode, swStReportErrorAlterApply.getStationCode())
                .eq(SwStReportErrorAlterApply::getReprotTime, swStReportErrorAlterApply.getReprotTime())
                .eq(SwStReportErrorAlterApply::getEquipmentCode, swStReportErrorAlterApply.getEquipmentCode());
        return R.ok(swStReportErrorAlterAppayMapper.update(sws,wrapper));
    }


    /**
     * 获取报表路径参数管理
     * @param param
     * @return
     */
    @RequestMapping("/getReports")
    @ApiOperation(value = "获取报表路径参数管理", notes = "获取报表路径参数管理")
    public Result getReports(@RequestParam Map<String,String> param){
        Result result=new Result(ResultDetailMsg.OK);
        try {
            //获取报表路径参数管理
            List<SwPmReportPath> reportErrorAlterAppays= reportMistakeInfoServiceImpl.getReports(param);
            result.setData(reportErrorAlterAppays);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("查询报表失败");
        }
        return result;
    }

    /**
     * 修改报表权限
     * @param param
     * @return
     */
    @RequestMapping("/update_xw_ReportPromise")
    @ApiOperation(value = "修改报表权限", notes = "修改报表权限")
    public Result update_xw_ReportPromise(@RequestParam Map<String,String> param){
        Result result=new Result(ResultDetailMsg.OK);
        try {
            List<SwPmReportPath> reportErrorAlterAppays= reportMistakeInfoServiceImpl.getReports(param);
            result.setData(reportErrorAlterAppays);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("查询报表失败");
        }
        return result;
    }

    /**
     * 修改报表
     * @param sheJiPersons
     * @return
     */
    @RequestMapping("/update_xw_ReportError")
    @ApiOperation(value = "修改报表", notes = "修改报表")
    public Result update_xw_ReportError(String row, String sheJiPersons){
        Result result=new Result(ResultDetailMsg.OK);
        try {

            SwStReportErrorAlterApply swStReportErrorAlterApply=JSONObject.parseObject(row, SwStReportErrorAlterApply.class);
            JSONObject param_exe=JSONObject.parseObject(row);
            param_exe.put("reportDate", swStReportErrorAlterApply.getReprotTime());
//            swStReportErrorAlterApply.setIsModify("1");
            if(StringUtils.isNotEmpty(swStReportErrorAlterApply.getBomCode())){
                //bom编号排序后修改
                swStReportErrorAlterApply.setBomCode(DateFormat.bomOrJobNoSort(swStReportErrorAlterApply.getBomCode()));
            }
            int aff = swStReportErrorAlterAppayMapper.updateById(swStReportErrorAlterApply);
            if(swStReportErrorAlterApply.getIsModify().equals("1")){ //是否修改，选择“是”的时候
                SwPmReportPath swPmReportPath = swPmReportPathMapper.selectById(swStReportErrorAlterApply.getReportNameId());
                if(swPmReportPath==null){
                    swPmReportPath=new SwPmReportPath();
                }
                String exeSql = swPmReportPath.getAfterUpdateReportSql();
                String params = swPmReportPath.getParams();
                JSONObject paramsJs = JSONObject.parseObject(params);
                paramsJs=paramsJs==null?new JSONObject():paramsJs;
                if(StringUtils.isNotEmpty(exeSql)){
                    JSONArray js=JSONArray.parseArray(exeSql);
                    for(int i=0;i<js.size();i++){
                        String sql = js.getString(i);
                        Matcher matcher=Pattern.compile("\\{(([a-z]|[A-Z]|[0-9])+)\\}").matcher(sql);
                        while(matcher.find()){
                            String group = matcher.group(1);//变量
                            Object o = paramsJs.get(group);
                            if(param_exe.get(group)!=null){
                                o=param_exe.get(group);
                            }
                            sql=sql.replaceAll("\\{"+group+"\\}","'"+o.toString()+"'");
                        }
                        Integer integer = JdbcUtil.exeSqlCrud(sql);
                    }
                }
            }
            Map<String,Object> param=new HashMap<>();
            param.put("relationid",swStReportErrorAlterApply.getId());
            swStReportErrorAlterAppayDetailMapper.deleteByMap(param);//删除

            JSONArray jsonArray = JSONArray.parseArray(sheJiPersons);
            for(int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                SwStReportErrorAlterAppayDetail swStReportErrorAlterAppayDetail=JSONObject.parseObject(jsonObject.toJSONString(), SwStReportErrorAlterAppayDetail.class);
                swStReportErrorAlterAppayDetail.setRelationid(swStReportErrorAlterApply.getId());
                swStReportErrorAlterAppayDetailMapper.insert(swStReportErrorAlterAppayDetail);
            }

        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            result.appendMsg("修改失败");
        }
        return result;
    }

//    @RequestMapping("updateReportApplyStatus")
//    @Transactional(rollbackFor = Exception.class)
//    public Map updateReportApplyStatus(@RequestParam Map<String,Object> params){
//        String checkStatus = String.valueOf(params.get("checkStatus"));
//        JSONArray ids = JSONObject.parseArray(String.valueOf(params.get("ids")));
//        try {
//            List<SwStReportErrorAlterApply> reportErrorAlterAppayOne = reportMistakeInfoMapper.selectBatchIds((List) ids);
//            for(SwStReportErrorAlterApply swStReportErrorAlterApply : reportErrorAlterAppayOne){
//                if(!"2".equals(swStReportErrorAlterApply.getCheckStatus())){
//                    continue;
//                }
//                swStReportErrorAlterApply.setCheckStatus(checkStatus);
//                reportMistakeInfoMapper.updateById(swStReportErrorAlterApply);
//            }
//            commonController.innerBatch(params);
//        }catch (Exception e){
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
//        Map resp = new HashMap();
//        resp.put("code","1");
//        return resp;
//    }


    /**
     * 线网报表申请
     * @param row
     * @return
     */
    @RequestMapping("/add_xw_sq_ReportError")
    @ApiOperation(value = "线网报表申请", notes = "线网报表申请")
    @Transactional(rollbackFor = Exception.class)
    public Result add_xw_sq_ReportError(String row, String sheJiPersons){
        Result result=new Result(ResultDetailMsg.OK);
        try {
            SwStReportErrorAlterApply swStReportErrorAlterApply=JSONObject.parseObject(row, SwStReportErrorAlterApply.class);
            //车站加入报表差错业务处理
            //车站申请添加 状态为1 车站业务模块申请添加并提交 状态为2
            addReprotError(swStReportErrorAlterApply,sheJiPersons,"1");
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            result.appendMsg("添加失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

    /**
     * 车站模块加入报表差错并提交
     * @param params
     * @return
     */
    @RequestMapping("/addAndSubmit_xw_sq_ReportError")
    @ApiOperation(value = "车站模块加入报表差错并提交", notes = "车站模块加入报表差错并提交")
    @Transactional(rollbackFor = Exception.class)
    public Result addAndSubmit_xw_sq_ReportError(@RequestBody JSONObject params){
        Result result=new Result(ResultDetailMsg.OK);
        try {
            SwStReportErrorAlterApply swStReportErrorAlterApply=JSONObject.parseObject(String.valueOf(params.get("row")), SwStReportErrorAlterApply.class);
            //车站加入报表差错业务处理
            //车站申请添加 状态为1 车站业务模块申请添加并提交 状态为2
            addReprotError(swStReportErrorAlterApply,String.valueOf(params.get("sheJiPersons")),"2");

            String dataSourceTable = swStReportErrorAlterApply.getDataSourceTable();
            Integer diffAmountId = swStReportErrorAlterApply.getDiffAmountId();
            //车站菜单业务自己加入报表差错
            if(diffAmountId != null && StringUtils.isNotEmpty(dataSourceTable) && Integer.parseInt(swStReportErrorAlterApply.getDataSource()) == 1){
                String sql = "UPDATE "+dataSourceTable+" SET IS_ADD_REPROT_ERROR = '1' where id = "+swStReportErrorAlterApply.getDiffAmountId();
                if("sw_st_stock".equalsIgnoreCase(swStReportErrorAlterApply.getDataSourceTable())){
                    String lineCode = swStReportErrorAlterApply.getLineCode();
                    String stationCode = swStReportErrorAlterApply.getStationCode();
                    String reprotDate = swStReportErrorAlterApply.getReprotTime();
                    sql = "UPDATE "+dataSourceTable+" SET IS_ADD_REPROT_ERROR = '1' " +
                            " WHERE LINE_CODE = '"+lineCode+"' " +
                            " and STATION_CODE = '"+stationCode+"' " +
                            " and CONFIRM_STATUS = '1' " +
                            " and STATISTICS_DATE = '"+reprotDate+"' ";
                }
                JdbcUtil.exeSqlCrud(sql);
            }else {
                throw new Exception("未加入成功！");
            }
            // TODO 需要组织param 发代办
            Map<String,Object> param = JSONObject.parseObject(String.valueOf(params.get("param")));
            commonController.innerBatch(param);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            result.appendMsg("添加失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

    /**
     * 车站加入报表差错通用方法
     * @param swStReportErrorAlterApply
     * @param sheJiPersons
     */
    public void addReprotError(SwStReportErrorAlterApply swStReportErrorAlterApply, String sheJiPersons, String checkStatus) {
        SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        swStReportErrorAlterApply.setApplyTime(dff.format(new Date()));
        swStReportErrorAlterApply.setErrorDetail("");
        //车站申请添加 状态为1 车站业务模块申请添加并提交 状态为2
        swStReportErrorAlterApply.setCheckStatus(checkStatus);
        swStReportErrorAlterApply.setCreateTime(dff.format(new Date()));
        Map<String,Object> p1=new HashMap<>();
        p1.put("user_code",UserInfo.getUserCode());
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
        swStReportErrorAlterApply.setCreatorName(sysUsers.get(0).getNikename());
        swStReportErrorAlterApply.setCreatorId(UserInfo.getId());
        swStReportErrorAlterApply.setDelFlag("0");
        swStReportErrorAlterApply.setUpdateTime(dff.format(new Date()));
        swStReportErrorAlterApply.setDataSource("1");
        if(StringUtils.isNotEmpty(swStReportErrorAlterApply.getBomCode())){
            //bom编号排序后新增
            swStReportErrorAlterApply.setBomCode(DateFormat.bomOrJobNoSort(swStReportErrorAlterApply.getBomCode()));
        }

        int aff = swStReportErrorAlterAppayMapper.insertReport(swStReportErrorAlterApply);

        JSONArray jsonArray = JSONArray.parseArray(sheJiPersons);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            SwStReportErrorAlterAppayDetail swStReportErrorAlterAppayDetail=JSONObject.parseObject(jsonObject.toJSONString(), SwStReportErrorAlterAppayDetail.class);
            swStReportErrorAlterAppayDetail.setRelationid(swStReportErrorAlterApply.getId());
            swStReportErrorAlterAppayDetailMapper.insert(swStReportErrorAlterAppayDetail);
        }
    }


    /**
     * 提交数据
     * @param param
     * @return
     */
    @RequestMapping("/submit_xw_ReportPromise")
    @ApiOperation(value = "提交数据", notes = "提交数据")
    @Transactional(rollbackFor = Exception.class)
    public Result submit_xw_ReportPromise(@RequestBody Map<String,Object> param){
        String ids = String.valueOf(param.get("idlist"));
        Result result=new Result(ResultDetailMsg.OK);
        try {
            List<SwStReportErrorAlterApply> swStReportErrorAlterApplies = swStReportErrorAlterAppayMapper.selectBatchIds(Arrays.asList(ids.split(",")));
            for(SwStReportErrorAlterApply swStReportErrorAlterApply:swStReportErrorAlterApplies){
                SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                swStReportErrorAlterApply.setUpdateTime(dff.format(new Date()));
                swStReportErrorAlterApply.setCheckStatus("2"); //待审批
                swStReportErrorAlterAppayMapper.updateById(swStReportErrorAlterApply);

                //车站申请报表差错--修改加入报表差错状态 如果datasource == 1 && id == null && dataSourceTable == null
                //false代表车站申请，true代表线网通知
                List<String> sqlList = stationReprotApply(swStReportErrorAlterApply,false);
                if(CollectionUtil.isNotEmpty(sqlList)){
                    for (String sql : sqlList) {
                        Integer integer = JdbcUtil.exeSqlCrud(sql);
                    }
                }
            }
            commonController.innerBatch(param);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            result.appendMsg("提交失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

    /**
     * 车站申请报表差错--修改加入报表差错状态
     * @param swStReportErrorAlterApply
     */
    public List<String> stationReprotApply(SwStReportErrorAlterApply swStReportErrorAlterApply,boolean flag) {
        //如果datasource == 1 && id == null && dataSourceTable == null
        List<String> listSql = new ArrayList<>();
        SwPmReportPath swPmReportPath = swPmReportPathMapper.selectById(swStReportErrorAlterApply.getReportNameId());
        if(ObjectUtil.isNotEmpty(swPmReportPath)){
            String stmSql = swPmReportPath.getStmSql();
            if(StringUtils.isNotEmpty(stmSql)){
                String dataSourceTable = swStReportErrorAlterApply.getDataSourceTable();
                //售票员票款差异主键ID
                Integer diffAmountId = swStReportErrorAlterApply.getDiffAmountId();
                //false代表车站申请，true代表线网通知
                boolean dataSourceFlag = false;
                if(flag){
                    //dataSourceFlag = StringUtils.isEmpty(dataSourceTable) && Integer.parseInt(swStReportErrorAlterApply.getDataSource()) > 1;
                    dataSourceFlag = true;
                }else {
                    dataSourceFlag = StringUtils.isEmpty(dataSourceTable) && diffAmountId == null && Integer.parseInt(swStReportErrorAlterApply.getDataSource()) == 1;
                }
                if(dataSourceFlag){
                    JSONObject jsonParam = (JSONObject) JSONObject.toJSON(swStReportErrorAlterApply,SerializeConfig.getGlobalInstance());
                    String params = swPmReportPath.getParams();
                    JSONObject paramsJs = JSONObject.parseObject(params);
                    paramsJs=paramsJs==null?new JSONObject():paramsJs;
                    JSONArray js=JSONArray.parseArray(stmSql);
                    for(int i=0;i<js.size();i++){
                        String sql = js.getString(i);
                        Matcher matcher=Pattern.compile("\\{(([a-z]|[A-Z]|[0-9])+)\\}").matcher(sql);
                        while(matcher.find()){
                            String group = matcher.group(1);//变量
                            Object o = paramsJs.get(group);
                            if(jsonParam.get(group)!=null){
                                o=jsonParam.get(group);
                            }
                            sql=sql.replaceAll("\\{"+group+"\\}","'"+o.toString()+"'");
                        }
                        listSql.add(sql);
                    }
                    return listSql;
                }
            }
        }
        return null;
    }
    /**
     * 删除数据（是否改为逻辑删除）
     * @param ids
     * @return
     */
    @RequestMapping("/deleteReportGGSQ")
    @ApiOperation(value = "删除数据（是否改为逻辑删除）", notes = "删除数据（是否改为逻辑删除）")
    public Result deleteReportGGSQ(String ids, @RequestParam Map<String,String> param){
        Result result=new Result(ResultDetailMsg.OK);
        try {
//            List<SwStReportErrorAlterApply> swStReportErrorAlterApplies = swStReportErrorAlterAppayMapper.selectBatchIds(Arrays.asList(ids.split(",")));
//            for(SwStReportErrorAlterApply swStReportErrorAlterApply:swStReportErrorAlterApplies){
//                SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                swStReportErrorAlterApply.setUpdateTime(dff.format(new Date()));
//                swStReportErrorAlterApply.setCheckStatus("2"); //待审批
//                swStReportErrorAlterAppayMapper.updateById(swStReportErrorAlterApply);
//            }
            //批量删除
            swStReportErrorAlterAppayMapper.deleteBatchIds(Arrays.asList(ids.split(",")));

        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            result.appendMsg("删除失败");
        }
        return result;
    }


    /**
     * 获取线网报表差错的报名名称信息
     * @param reportId 报表名字的id
     * @return
     */
    @RequestMapping("/get_xw_ReportInfos")
    @ApiOperation(value = "获取线网报表差错的报名名称信息", notes = "获取线网报表差错的报名名称信息")
    public Result get_xw_ReportInfos(String reportId, @RequestParam Map<String,String> param){
        Result result=new Result(ResultDetailMsg.OK);
        try {
            SwPmReportPath swPmReportPath = swPmReportPathMapper.selectById(Integer.parseInt(param.get("reportId")));
            List<Map<String, Object>> maps = JdbcUtil.exeSqlQuery("select group_concat(STATION_PERSONR_CODE) stationPersonrCode from sw_st_report_error_alter_appay_detail where RELATIONID=" + param.get("id"));
            Object stationPersonCode="";
            if(maps.size()>0){
                stationPersonCode=maps.get(0).get("stationPersonrCode");
            }
            JSONObject res=JSONObject.parseObject(JSONObject.toJSONString(swPmReportPath));
            res.put("stationPersonCode",stationPersonCode);
            result.setData(res);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("查询报表出错");
        }
        return result;
    }

    /**
     * 修改报表信息
     * @param reportId
     * @return
     */
    @RequestMapping("/update_xw_ReportInfos")
    @ApiOperation(value = "修改报表信息", notes = "修改报表信息")
    public Result update_xw_ReportInfos(String reportId, @RequestParam Map<String,String> param){
        Result result=new Result(ResultDetailMsg.OK);
        try {
            SwPmReportPath swPmReportPath = swPmReportPathMapper.selectById(Integer.parseInt(param.get("reportId")));
            String exeSql = swPmReportPath.getExeSql();
            String params = swPmReportPath.getParams();
            JSONObject paramsJs = JSONObject.parseObject(params);
            paramsJs=paramsJs==null?new JSONObject():paramsJs;
            JSONArray js=JSONArray.parseArray(exeSql);
            for(int i=0;i<js.size();i++){
                String sql = js.getString(i);
                Matcher matcher=Pattern.compile("\\{(([a-z]|[A-Z]|[0-9])+)\\}").matcher(sql);
                while(matcher.find()){
                    String group = matcher.group(1);//变量
                    Object o = paramsJs.get(group);
                    if(param.get(group)!=null){
                        o=param.get(group);
                    }
                    sql=sql.replaceAll("\\{"+group+"\\}","'"+o.toString()+"'");
                }
                Integer integer = JdbcUtil.exeSqlCrud(sql);
                log.info(sql);
            }

            String reportPath = swPmReportPath.getReportPath();
            swPmReportPath.setReportPath(reportPath.split("\\?")[0]);
            JSONObject res=JSONObject.parseObject(JSONObject.toJSONString(swPmReportPath));
            res.put("param",getUrlmap(reportPath,param));
            result.setData(res);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("修改报表出错");
        }
        return result;
    }

   private Map<String,String> getUrlmap(String url,Map<String,String> param){
       String[] split = url.split("\\?");
       String pa = split[1];
       Map<String,String> p=new HashMap<>();
       for(String d:pa.split("&")){
           String v = d.split("=")[1];
           String value="";
           if(!v.contains("{") && !v.contains("}")) {
               value=v;
           }else if(v.contains("{") && v.contains("}")){
               value = param.get(v.replaceAll("[{|}]+", ""));
           }
           p.put(d.split("=")[0],value);
       }
       return p;
   }


}
